package com.lg.system.log.service.impl;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.lang.reflect.Method;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.AuthorizationException;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.lg.common.annotation.Logs;
import com.lg.common.exception.GlobalException;
import com.lg.system.log.dao.LogDao;
import com.lg.system.log.entity.Log;
import com.lg.system.log.service.LogService;
import com.lg.system.user.entity.UserEntity;
import com.lg.utils.HttpContextUtils;
import com.lg.utils.JSONUtils;
import com.lg.utils.RequestUtils;

/**
 * <p>
 * 系统日志 服务实现类
 * </p>
 *
 * @author dl
 * @since 2018-03-27
 */
@Service
public class LogServiceImpl extends ServiceImpl<LogDao, Log> implements LogService {

	@Override
	public void saveLog(ProceedingJoinPoint joinPoint,long time) {
		MethodSignature signature = (MethodSignature) joinPoint.getSignature();
		Date date = new Date();
		Method method = signature.getMethod();
		Log sysLog = new Log();
		Logs logs = method.getAnnotation(Logs.class);
		if (logs != null) {
			sysLog.setOperation(logs.value());
			sysLog.setModule(logs.module());
		}
		// 请求的方法名
		String className = joinPoint.getTarget().getClass().getName();
		String methodName = signature.getName();
		sysLog.setMethod(className + "." + methodName + "()");
		// 请求的参数
		Object[] args = joinPoint.getArgs();
		try {
			String params = JSONUtils.beanToJson(args[0]).substring(0, 4999);
			sysLog.setParams(params);
		} catch (Exception e) {

		}
		// 获取request
		HttpServletRequest request = HttpContextUtils.getHttpServletRequest();
		// 设置IP地址
		sysLog.setIp(RequestUtils.getIpAddr(request));
		// 用户名
		UserEntity userEntity=(UserEntity)SecurityUtils.getSubject().getPrincipal();
		if (null != userEntity) {
			sysLog.setUserId(userEntity.getUserId());
			sysLog.setUsername(userEntity.getUsername());
		} 
		sysLog.setTime((int) time);
		sysLog.setGmtCreate(date);
		sysLog.setType(0);
		insert(sysLog);
	}

	@Override
	public void saveLog(Exception e) {
		Date date = new Date();
		Log sysLog = new Log();
		sysLog.setType(1);
		if(e.getCause()!=null){
			sysLog.setMethod(e.getCause().getMessage());
			sysLog.setModule(e.getCause().getClass().getName());
		}else{
			sysLog.setModule(e.getMessage());
		}
		sysLog.setGmtCreate(date);
		UserEntity userEntity=(UserEntity)SecurityUtils.getSubject().getPrincipal();
		if (null != userEntity) {
			sysLog.setUserId(userEntity.getUserId());
			sysLog.setUsername(userEntity.getUsername());
		} 
		HttpServletRequest request = HttpContextUtils.getHttpServletRequest();
		sysLog.setIp(RequestUtils.getIpAddr(request));
		sysLog.setTime(0);
		ByteArrayOutputStream baos = new ByteArrayOutputStream();  
		e.printStackTrace(new PrintStream(baos));  
		String exception = baos.toString();  
		if(e instanceof AuthorizationException){
			sysLog.setOperation(exception+"shiro 权限异常");
		}else if(e instanceof GlobalException){
			sysLog.setOperation(exception+"验证异常");
		}else{
			sysLog.setOperation(exception+"其他异常");
		}
		insert(sysLog);
	}

}
